package com.ruoyi.sxt.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SxtBumen;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sxt.mapper.SxtBumenMapper;
import com.ruoyi.sxt.service.ISxtBumenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 部门Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-25
 */
@Service
public class SxtBumenServiceImpl implements ISxtBumenService
{
    @Autowired
    private SxtBumenMapper sxtBumenMapper;

    /**
     * 查询部门
     * 
     * @param deptId 部门主键
     * @return 部门
     */
    @Override
    public SxtBumen selectSxtBumenByDeptId(Long deptId)
    {
        return sxtBumenMapper.selectSxtBumenByDeptId(deptId);
    }

    /**
     * 查询部门列表
     * 
     * @param sxtBumen 部门
     * @return 部门
     */
    @Override
    public List<SxtBumen> selectSxtBumenList(SxtBumen sxtBumen)
    {
        return sxtBumenMapper.selectSxtBumenList(sxtBumen);
    }

    @Override
    public List<Ztree> selectDeptTree(SxtBumen sxtBumen) {
        List<SxtBumen> bumenList = sxtBumenMapper.selectSxtBumenList(sxtBumen);
        List<Ztree> ztrees = initZtree(bumenList);
        return ztrees;
    }

    public List<Ztree> initZtree(List<SxtBumen> bumenList)
    {
        return initZtree(bumenList, null);
    }

    public List<Ztree> initZtree(List<SxtBumen> bumenList, List<String> roleDeptList)
    {

        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleDeptList);
        for (SxtBumen bumen : bumenList)
        {
            if (UserConstants.DEPT_NORMAL.equals(bumen.getStatus()))
            {
                Ztree ztree = new Ztree();
                ztree.setId(bumen.getDeptId());
                ztree.setpId(bumen.getParentId());
                ztree.setName(bumen.getDeptName());
                ztree.setTitle(bumen.getDeptName());
                if (isCheck)
                {
                    ztree.setChecked(roleDeptList.contains(bumen.getDeptId() + bumen.getDeptName()));
                }
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }

    /**
     * 新增部门
     * 
     * @param sxtBumen 部门
     * @return 结果
     */
    @Override
    public int insertSxtBumen(SxtBumen sxtBumen)
    {
        SxtBumen info = sxtBumenMapper.selectSxtBumenByDeptId(sxtBumen.getParentId());
        sxtBumen.setAncestors(info.getAncestors() + "," + sxtBumen.getParentId());
        sxtBumen.setCreateTime(DateUtils.getNowDate());
        return sxtBumenMapper.insertSxtBumen(sxtBumen);
    }

    /**
     * 修改部门
     * 
     * @param sxtBumen 部门
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSxtBumen(SxtBumen sxtBumen)
    {
        SxtBumen newParentDept = sxtBumenMapper.selectSxtBumenByDeptId(sxtBumen.getParentId());
        SxtBumen oldDept = selectSxtBumenByDeptId(sxtBumen.getDeptId());
        if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
        {
            String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
            String oldAncestors = oldDept.getAncestors();
            sxtBumen.setAncestors(newAncestors);
            updateDeptChildren(sxtBumen.getDeptId(), newAncestors, oldAncestors);
        }
        sxtBumen.setUpdateTime(DateUtils.getNowDate());
        return sxtBumenMapper.updateSxtBumen(sxtBumen);
    }

    public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
    {
        List<SxtBumen> children = sxtBumenMapper.selectChildrenDeptById(deptId);
        for (SxtBumen child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            sxtBumenMapper.updateDeptChildren(children);
        }
    }

    /**
     * 批量删除部门
     * 
     * @param deptIds 需要删除的部门主键
     * @return 结果
     */
    @Override
    public int deleteSxtBumenByDeptIds(String deptIds)
    {
        return sxtBumenMapper.deleteSxtBumenByDeptIds(Convert.toStrArray(deptIds));
    }

    /**
     * 删除部门信息
     * 
     * @param deptId 部门主键
     * @return 结果
     */
    @Override
    public int deleteSxtBumenByDeptId(Long deptId)
    {
        return sxtBumenMapper.deleteSxtBumenByDeptId(deptId);
    }
}
